import { routerRedux } from 'dva/router';
import { addCar, delCarAll, delCarGoods, editCar, getCarList } from '../services/car';
import { postOrderToServer, copyToCar, cancelOrder } from '../services/order';
// import {getCarAllData} from './global';


export default {
  namespace: 'car',
  state: {
    carList: [],
    nextPage: 5,
    pageNum: 0,
    customerId: 1,
    isLastPage: false,
  },

  effects: {
    *editCarData({ payload }, { call, put }) {
      const response = yield call(editCar, payload);
      if (response.code === 0) {
        yield put({
          type: 'editData',
          payload,
        });
      }
    },
    *delectCarData({ payload }, { call, put }) {
      const response = yield call(delCarGoods, payload);
      if (response.code === 0) {
        yield put({
          type: 'delData',
          payload,
        });
        yield put({
          type: 'global/getCarAllData',
          payload: { customerId: 1 },
        });
      }
    },
    *getCarData({ payload }, { call, put }) {
      const response = yield call(getCarList, payload);
      if (response.code === 0) {
        yield put({
          type: 'changeData',
          payload: response.data,
        });
      }
    },
    *delectCarAllData({ payload }, { call, put }) {
      const response = yield call(delCarAll, payload);
      if (response.code === 0) {
        yield put({
          type: 'delAllData',
        });
        yield put({
          type: 'global/getCarAllData',
          payload: { customerId: 1 },
        });
      }
    },
    *postCarOrderToSever({ payload }, { call, put }) {
      const response = yield call(postOrderToServer, payload);
      console.log(response);
      if (response.code === 0) {
        yield put(routerRedux.push('/success'));
      } else {
        alert('出错了!!!');
      }
    },
  },

  reducers: {
    changeData(state, { payload }) {
      let dataList = [];
      if (state.carList.length < payload.list || state.carList.length === 0) {
        dataList = payload.list;
      } else {
        dataList = state.carList;
        dataList = dataList.concat(payload.list);
      }
      return {
        ...state,
        carList: dataList,
        nextPage: payload.nextPage,
        pageNum: payload.pageNum,
        customerId: payload.list[0].customerId,
        isLastPage: payload.isLastPage,
      };
    },
    editData(state, action) {
      let dataList = [];
      dataList = state.carList;
      dataList = dataList.filter((item) => {
        if (item.internalTradeId === action.payload.internalTradeId) {
          item.count = action.payload.count;
        }
        return item;
      });
      return {
        ...state,
        carList: dataList,
        isEdit: true,
      };
    },
    delData(state, action) {
      let dataList = [];
      dataList = state.carList;
      dataList = dataList.filter((item) => {
        if (item.internalTradeId !== action.payload.internalTradeId) {
          return item;
        }
      });
      return {
        ...state,
        carList: dataList,
        isEdit: false,
      };
    },
    delAllData(state, action) {
      return {
        ...state,
        carList: [],
        isEdit: false,
      };
    },
    getCarDataToPost(state, action) {
      console.log(11111, state);
      console.log(22222, action);
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      // dispatch({
      //   type: 'getCarAllData',
      //   payload: {
      //     customerId: 1,
      //     pageNum: 0,
      //   },
      // });
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      // return history.listen(({ pathname, search }) => {
      //   if (typeof window.ga !== 'undefined') {
      //     window.ga('send', 'pageview', pathname + search);
      //   }
      // });
      history.listen((location) => {
        if (location.pathname.includes('shoppingCar')) {
          dispatch({
            type: 'delAllData',
          });
          dispatch({
            type: 'getCarData',
            payload: { pageNum: 1, pageSize: 10, customerId: 1 },
          });
          // dispatch({
          //   type: 'global/getTags',
          // });
        }
      });
    },
  },
};
